package cn.js.tech;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import lombok.extern.slf4j.Slf4j;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class CrackerTransformer implements ClassFileTransformer {

    List<String> classes = new ArrayList<String>(Arrays.asList(
            "org.apache.jsp.designer.taskMyProperties_jsp",
            "org.jeecg.modules.online.desform.b.a",
            "org.apache.commons.lang.StreamUtils",
            "org.apache.commons.lang.StringUtil",
            "org.jeecg.modules.online.desform.a.c",
            "org.jeecg.modules.online.desform.service.impl.d",
            "org.jeecg.modules.jmreport.visual.util.b",
            "org.jeecg.modules.jmreport.visual.service.impl.a",
            "org.jeecg.modules.extbpm.a.a",
            "org.jeecg.modules.bpm.service.a.c",
            "org.apache.commons.lang.StringUtil",
            "org.jeecg.modules.bpm.service.a.d",
            "org.jeecg.modules.extbpm.process.service.impl.n",
            "org.jeecg.designer.a.c",
            "org.jeecg.designer.a.a",
            "org.jeecg.designer.a.b",
            "org.jeecg.modules.online.cgform.d.c",
            "org.jeecg.modules.online.cgform.service.impl.i",
            "org.jeecg.modules.online.cgform.service.impl.a"

    ));

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
                            ProtectionDomain protectionDomain, byte[] classfileBuffer) {


        try {
            if (className != null && classes.contains(className.replace("/", "."))) {
                CtClass ctClass = clearStaticBlock(className);
                return ctClass.toBytecode();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (className != null && className.equals("org.jeecg.designer.service.a".replace(".", "/"))) {

            try {
                ClassPool classPool = ClassPool.getDefault();
                CtClass ctClass = classPool.get("org.jeecg.designer.service.a");

                for (CtMethod method : ctClass.getMethods()) {
                    log.info(method.getLongName());
                }

                CtMethod ctMethod = ctClass.getDeclaredMethod("a",null);
                ctMethod.setBody("{ return null;  }");

//                Files.write(Paths.get("E:\\" + className.replace("/", "-") + ".class"), ctClass.toBytecode());

                return ctClass.toBytecode();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    private static CtClass clearStaticBlock(String className) throws Exception {
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.get(className.replace("/", "."));
        CtConstructor staticConstructor = ctClass.getClassInitializer();
        staticConstructor.setBody("{  }");
        return ctClass;
    }


}

